package nuc.soft.xsf.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import nuc.soft.xsf.model.Post;
import nuc.soft.xsf.model.PostReply;
import nuc.soft.xsf.model.PostSupport;
import nuc.soft.xsf.model.User;
import nuc.soft.xsf.service.IPostReplyService;
import nuc.soft.xsf.service.IPostSendService;
import nuc.soft.xsf.service.IPostService;
import nuc.soft.xsf.service.IPostSupportService;
import nuc.soft.xsf.service.IUserService;
import nuc.soft.xsf.tools.Tools;

@Service
public class PostSupportService implements IPostSupportService {

	@Autowired
	private IPostService postDao;
	
	@Autowired
	private IPostReplyService postReplyDao;
	
	@Autowired
	private IUserService userDao;
	
	@Autowired
	private IPostSendService postSendDao;
	
	
	
	public JSONArray findByConditionJSON(PostReply postReply)
	{
		JSONArray jsonArray=new JSONArray();
		Map map=Tools.transBean2Map(postReply);
		List<PostReply> postList=postReplyDao.findByCondition(map);
	    JSONObject json=new JSONObject();
	    for(int i=0;i<postList.size();i++){
	    	Post post=postDao.findById(postList.get(i).getPostId());
	    	User u=userDao.findById(postList.get(i).getUserId());
	    	User u1=userDao.findById(postList.get(i).getTouserId());
	    	json.put("id", post.getPostId());
	    	json.put("fromUserName", u.getUserName());
	    	json.put("toUser", u1.getUserName());
	    	json.put("recieve", post.getReceiveTime());
	    	json.put("content",post.getPostContent());
	    	/*if(postReply.getTopostId()==post.getPostId()){
	    		json.put("state","1");
	    	}
	    	else{
	    		json.put("state", "0");
	    	}*/
	    	jsonArray.add(json);
	    }
		
		return jsonArray;
	}
	
	
	
	public List findByCondition(PostReply postReply)
	{   
		Map map=Tools.transBean2Map(postReply);
		List<PostReply> postList=postReplyDao.findByCondition(map);
		List<PostSupport> supportlist=new ArrayList<PostSupport>();
		for(int i=0;i<postList.size();i++){
			 	Post post=postDao.findById(postList.get(i).getPostId());
		    	User u=userDao.findById(postList.get(i).getUserId());
		    	User u1=userDao.findById(postList.get(i).getTouserId());
		    	PostSupport support=new PostSupport();
		    	support.setId(""+post.getPostId());
		    	support.setFromUserName(u.getUserName());
		    	support.setToUser(u1.getUserName());
		    	support.setRecieve(post.getReceiveTime());
		    	support.setContent(post.getPostContent());
		    	support.setUserImage(u.getUserImage());
		    	System.out.println("lllllllllll"+u.getUserImage());
		    	supportlist.add(support);
		 }
		 
		 return supportlist;
	}
	
}
